import 'package:dio/dio.dart';
import 'package:sinosheets/utils/common_utils/log_util.dart';
import 'package:sinosheets/utils/storage/LSUtil.dart';

/// 全局错误处理器
class ErrorHandler {
  /// 原始错误信息
  static String _getOriginalErrorMessage(dynamic error) {
    if (error is DioException) {
      return '''
Original DioError:
Type: ${error.type}
Message: ${error.message}
Path: ${error.requestOptions.path}
Response: ${error.response?.data}
StackTrace: ${error.stackTrace}
''';
    } else {
      return '''
Original Error:
Type: ${error.runtimeType}
Message: ${error.toString()}
''';
    }
  }

  /// 处理网络错误
  static void handleNetworkError(DioException error) {
    if (LSUtil.getAppEnv() == 'prod') {}
    LogUtil.e(_getOriginalErrorMessage(error));
  }

  /// 处理业务错误
  static void handleBusinessError(int code, String message, {dynamic data, Map<String, dynamic>? rawData}) {
    if (LSUtil.getAppEnv() == 'prod') {}
    LogUtil.e('BusinessError: code=$code, message=$message, data=$data, rawData=$rawData');
  }

  /// 处理未知异常
  static void handleUnknownError(dynamic error) {
    if (LSUtil.getAppEnv() == 'prod') {}
  }
}
